import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Demo_MyThreadPool {
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(10);
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    //使用构造方法创建线程，消费队列中的任务
    public Demo_MyThreadPool(int n){
        for(int i = 0; i < n; i++){
            Thread t = new Thread(() -> {
                try {
                    Runnable runnable = queue.take();
                    runnable.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }
}

class Test{
    public static void main(String[] args) throws InterruptedException {
        Demo_MyThreadPool myThreadPoll = new Demo_MyThreadPool(4);
        for(int i = 0; i < 1000; i++){
            int id = i;
            myThreadPoll.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行任务： " + id);
                }
            });
        }
    }
}
